import React, { Component } from "react";
import { Button, message } from "antd";
import { messages } from "share/common";
import errorMessage from "share/errorMessage";
import menuRoute from "routes/menuRoute";
import Condition from "components/condition";
import ApproveBar from "components/template/approve-bar";
import budgetJournalService from "./budget-journal.service";

const { If, Else } = Condition;

class BudgetJournalApproveButtons extends Component {
  state = {
    loading: false,
  };

  // 审批通过
  handleApprovePass = ({ reason, honestEvent, additionalItems, signatureAttachmentOID }) => {
    const { approverOID, entityOID } = this.props;
    const params = {
      approvalTxt: reason,
      entities: [
        {
          approverOID,
          entityOID,
          entityType: 3005,
          countersignApproverOIDs: additionalItems.map((item) => item.userOID),
        },
      ],
      conditionIds: honestEvent.map((item) => item.id),
      signatureAttachmentOID: signatureAttachmentOID || undefined,
    };
    this.setState({ loading: true });
    budgetJournalService
      .handleJournalApprovePass(params)
      .then((res) => {
        if (res.data.failNum === 0) {
          message.success(messages("common.operate.success") /*操作成功*/);
          this.goBack(true);
        } else {
          message.error(`${messages("common.operate.filed") /*操作失败*/}，${res.data.failReason[entityOID]}`);
        }
      })
      .catch((e) => errorMessage(e.response))
      .finally(() => this.setState({ loading: false }));
  };

  // 审批驳回
  handleApproveReject = ({ reason, honestEvent, additionalItems, signatureAttachmentOID }) => {
    const { approverOID, entityOID } = this.props;
    let params = {
      approvalTxt: reason,
      entities: [
        {
          approverOID,
          entityOID,
          entityType: 3005,
          countersignApproverOIDs: additionalItems.map((item) => item.userOID),
        },
      ],
      conditionIds: honestEvent.map((item) => item.id),
      signatureAttachmentOID: signatureAttachmentOID || undefined,
    };
    this.setState({ loading: true });
    budgetJournalService
      .handleJournalApproveReject(params)
      .then((res) => {
        const { failNum, failReason } = res.data;
        if (failNum !== 0) {
          message.error(`${messages("common.operate.filed") /*操作失败*/}，${failReason[entityOID]}`);
        } else {
          message.success(messages("common.operate.success") /*操作成功*/);
          this.goBack(true);
        }
      })
      .catch((e) => errorMessage(e.response))
      .finally(() => this.setState({ loading: false }));
  };

  goBack = (needRefresh) => {
    const { close } = this.props;
    if (close) {
      close(needRefresh);
    } else {
      menuRoute.goBack(this)
    }
  };

  render() {
    const { isWaitToApprove } = this.props;
    const { loading } = this.state;
    return (
      <div className="bottom-bar">
        <Condition>
          <If value={isWaitToApprove}>
            <ApproveBar
              allLoading={loading}
              buttons={["pass", "reject"]}
              handleApprovePass={this.handleApprovePass}
              handleApproveReject={this.handleApproveReject}
              goBack={this.goBack}
            />
          </If>
          <Else>
            <Button onClick={() => this.goBack()}>{messages("common.back") /*返回*/}</Button>
          </Else>
        </Condition>
      </div>
    );
  }
}

BudgetJournalApproveButtons.contextTypes = {
  router: React.PropTypes.object,
};

export default BudgetJournalApproveButtons;
